//
//  CinemaViewController.m
//  DouBan-Cinema
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaView.h"
#import "CinemaTableViewCell.h"

#import "CinemaModel.h"
#import "DataHandle.h"
@interface CinemaViewController ()<UITableViewDataSource,UITableViewDelegate,dataHandleDelegate>

@end

@implementation CinemaViewController

- (void)loadView
{
    self.cinemaView = [[CinemaView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.cinemaView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"影院";
//    self
    //设置代理
    self.cinemaView.tableView.dataSource = self;
    self.cinemaView.tableView.delegate = self;
    __weak typeof(self) weakSelf = self;
    [[DataHandle sharedInstance] getCinemaDataSourceReloadBlack:^(id object){
        [weakSelf.cinemaView.tableView reloadData];
    }];
}

#pragma mark --- 实现代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[DataHandle sharedInstance] rowOfCinema];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cinemaStr = @"cinemaStr";
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cinemaStr];
    if (!cell) {
        cell = [[CinemaTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cinemaStr];
    }
    CinemaModel *cinemaModel = [[DataHandle sharedInstance] cinemaModerForIndexPath:indexPath];
    cell.cinema = cinemaModel;
    float y = cell.addressLabel.frame.size.height;
    float height = [CinemaViewController heightForLabelText:cinemaModel.address];
    CGRect rect = cell.addressLabel.frame;
    rect.size.height = height;
    cell.addressLabel.frame = rect;
    CGRect rectPhone = cell.telephoneLabel.frame;
    rectPhone.origin.y -= (y - height);
    cell.telephoneLabel.frame = rectPhone;
    
    rect = cell.imageViewCell.frame;
    rect.size.height -= (y - height);
    cell.imageViewCell.frame = rect;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 + [CinemaViewController heightForLabelText:[[[DataHandle sharedInstance] cinemaModerForIndexPath:indexPath] address]] - 35;
}

+ (CGFloat)heightForLabelText:(NSString *)text
{
    CGSize size = CGSizeMake(335, 1000);
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
